﻿using System;
using System.Collections.Generic;
using System.Security.Principal;
using Microsoft.Practices.ServiceLocation;

using PMngr.Data;
using PMngr.Infrastructure;

namespace PMngr.Main
{
    public class PMIdentity : IIdentity
    {
        // Add private fields to store the user name and a value that indicates 
        // if the user is authenticated
        private string nameValue; 
        private bool authenticatedValue;
        private List<BuiltInRole> roles = new List<BuiltInRole>();

        /// <summary>
        /// Initializes the class by authenticating the user and then setting the 
        /// user name and role, based on a name and a password
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        public PMIdentity(string name, string password)
        {
            var status = ValidNameAndPassword(name, password);
            if(status == UserStatus.Valid)
            {
                this.nameValue = name;
                this.authenticatedValue = true;
            }
            else
            {
                this.nameValue = "anonymouse";
                this.authenticatedValue = false;
            }
        }      

        private UserStatus ValidNameAndPassword(string username, string password)
        {
            var repositoryFactory = ServiceLocator.Current.GetInstance<IRepositoryFactory>();
            using (var repository = repositoryFactory.CreateRepository())
            {
                var user = repository.Single<User>(u => u.Name == username);

                if (user == null)
                    return UserStatus.InValid;

                bool status = password.VerifyMD5(user.EncryptPwd);
                if (status)
                {
                    var br = (BuiltInRole) Enum.Parse(typeof (BuiltInRole), user.Role.ToString());
                    roles = new List<BuiltInRole> {br};
                    return UserStatus.Valid;
                }

                return UserStatus.WrongPassword;
            }
        }

        /// <summary>
        /// Returns a string that indicates the current authentication mechanism
        /// </summary>
        public string AuthenticationType
        {
            get
            {
                return "Custom Authentication";
            }
        }

        /// <summary>
        /// Returns a value that indicates whether the user has been authenticated
        /// </summary>
        public bool IsAuthenticated
        {
            get
            {
                return this.authenticatedValue;
            }
        }

        /// <summary>
        /// Returns the user's name
        /// </summary>
        public string Name
        {
            get
            {
                return this.nameValue;
            }
        }

        /// <summary>
        /// Returns the user's roles
        /// </summary>
        public List<BuiltInRole> Roles
        {
            get
            {
                return this.roles;
            }
        } 
    }
}


